package com.polaris.live.ui.party.widget.videoseat.base

import android.content.Context
import android.util.AttributeSet
import android.view.TextureView
import android.widget.FrameLayout
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.constant.PagConst
import com.polaris.live.common.util.JsonUtils
import com.polaris.live.im.sfs.bean.live.LiveRoomInfo
import com.polaris.live.im.sfs.bean.live.SendLiveMessage
import com.polaris.live.im.sfs.bean.live.ext.UserBasicVo
import com.polaris.live.resp.back_resp.UserRoomThemeResp
import com.polaris.live.ui.party.widget.seat.SeatBeanUtil
import com.polaris.live.ui.party.widget.videoseat.bean.VideoSeatBean
import com.polaris.live.widget.bubble.DownLoadFileUtil

/**
 *
 * @author Created by 天晴 on 2024/6/22 10:09
 * @since 1.0.0
 **/
abstract class BaseVideoSeatItemView(context: Context, attrs: AttributeSet?) : FrameLayout(context, attrs) {

    /**
     * 座位ID
     */
    var mSeatId: Int = -1
    lateinit var seatBean: VideoSeatBean

    private var defaultSoundPath: String = PagConst.LIVE_PARTY_SEAT //默认音浪地址
    private var soundEffectPath: String? = null //座位用户佩戴了声浪远程地址
    private var soundLocalPath: String? = null //座位用户佩戴了声浪本地地址

    /**
     * 麦位锁定
     */
    var mSiteLock: Boolean = false

    fun initView(seatId: Int) {
        mSeatId = seatId
        seatBean = VideoSeatBean(seatId)
        initView()
        upDataSoundPath(defaultSoundPath)
    }

    fun setData(mLiveInfo: LiveRoomInfo) {
        if (mSeatId == -1) {
            throw Exception("mSeatId is no init")
        }
        seatBean.apply {
            val userJson = SeatBeanUtil.getUserJson(mLiveInfo, mSeatId)
            if (userJson != userData) { //两个数据不相等再设置
                userData = userJson
                if (!userJson.isNullOrEmpty()) {
                    userBasicVo = JsonUtils.fromJson(userJson, UserBasicVo::class.java)
                }
            }
            mSiteLock = SeatBeanUtil.getLock(mLiveInfo, mSeatId) ?: false
            siteLock = mSiteLock
            siteMicStatus = SeatBeanUtil.getMicStatus(mLiveInfo, mSeatId)
            siteGainCoin = SeatBeanUtil.getGainCoin(mLiveInfo, mSeatId)
            siteStreamId = SeatBeanUtil.getStreamId(mLiveInfo, mSeatId)
            siteVideoStatus = SeatBeanUtil.getSiteVideoStatus(mLiveInfo, mSeatId)

            if (soundEffectPath != userBasicVo?.soundEffect) {
                soundEffectPath = userBasicVo?.soundEffect
                val cleanUrl = soundEffectPath?.substringBefore("?")
                DownLoadFileUtil.loadPag(cleanUrl) { nPath, realPath ->
                    if (cleanUrl == nPath) {
                        soundLocalPath = realPath
                        upDataSoundPath(realPath)
                    }
                }
            }
            if (userBasicVo == null) {
                soundLocalPath = null
                upDataSoundPath(defaultSoundPath)
            }
        }
        upDataView(seatBean)
    }

    /**
     * 是不是自己
     */
    private fun isOwn(): Boolean {
        return seatBean.userBasicVo?.userId == UserManager.getUserId()
    }

    fun getSeatId(): Int {
        return mSeatId
    }

    fun getData(): VideoSeatBean {
        return seatBean
    }

    fun resetView() {
        seatBean.apply {
            userData = null
            userBasicVo = null
            siteStreamId = null
            siteGainCoin = null
            siteMicStatus = null
        }
        soundLocalPath = null
        mSiteLock = false
        upDataView(seatBean, true)
        upDataSoundPath(defaultSoundPath)
    }

    /**
     * 更新数据
     */
    abstract fun upDataView(mData: VideoSeatBean, isReset: Boolean = false)

    /**
     * 展示emoji
     */
    abstract fun showEmoji(message: SendLiveMessage)

    /**
     * 获取视频播放控件
     */
    abstract fun getTextureView(): TextureView

    /**
     * 修改职位
     */
    abstract fun upDataUserPosition(position: Int)

    abstract fun initView()

    /**
     * 更新音浪
     */
    abstract fun soundLevelUpdate(soundLevel: Float)

    /**
     * 更新音浪pag
     */
    abstract fun upDataSoundPath(soundPath: String)

    /**
     * 更新皮肤
     */
    abstract fun upDateSkinView(userRoomThemeResp: UserRoomThemeResp? = null)
}